package com.sxfq.loansupermarket.app.platform.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.sxfq.loansupermarket.app.constants.AppConstant;
import com.sxfq.loansupermarket.app.dto.AppResponseResult;
import com.sxfq.loansupermarket.app.platform.service.PlatformService;
import com.sxfq.loansupermarket.common.utils.NumberUtil;
import com.sxfq.loansupermarket.common.utils.StringUtil;

/**
 * 平台控制器
 * 
 * 
 * Module:
 * 
 * IndexController.java
 * 
 * @author 程盼
 * @since JDK 1.8
 * @version 1.0
 * @description: <描述>
 */
@Controller
@RequestMapping(value = "/platform")
public class PlatformController {

	private Logger logger = Logger.getLogger(PlatformController.class);

	@Resource
	private PlatformService platFormService;

	/**
	 * 查询首页产品
	 * 
	 * @param request
	 * @param httpServletResponse
	 * @return
	 */
	@RequestMapping(value = "getPlatformList")
	@ResponseBody
	public AppResponseResult getPlatformList(HttpServletRequest request, HttpServletResponse response) {
		AppResponseResult result = new AppResponseResult();
		try {
			Integer pageNum = NumberUtil.parseInteger(request.getParameter("pageNum"), null);// 页码
			Integer pageSize = NumberUtil.parseInteger(request.getParameter("pageSize"), null);// 每页数量
			logger.info("【PlatformController.getPlatform】pageNum:" + StringUtil.toString(pageNum) + ",pageSize:"
					+ StringUtil.toString(pageSize));
			if (pageNum == null || pageSize == null || pageNum <= 0 || pageSize <= 0) {
				result.setCode(AppConstant.Code.FAIL);
				result.setMsg(AppConstant.Msg.FAIL);
				result.setResult("");
				return result;
			}
			List<Map<String, Object>> list = platFormService.getPlatformList(pageNum, pageSize);
			logger.info("【PlatformController.getPlatformList】list:" + JSONArray.toJSONString(list));
			result.setCode(AppConstant.Code.SUCCESS);
			result.setMsg(AppConstant.Msg.SUCCESS);
			result.setResult(list);
		} catch (Exception e) {
			result.setCode(AppConstant.Code.ERROR);
			result.setMsg(AppConstant.Msg.ERROR);
			result.setResult("");
			logger.error("【PlatformController.getPlatformList】异常", e);
		}
		return result;
	}

	/**
	 * 查询推荐产品
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "getRecommendPlatformList")
	@ResponseBody
	public AppResponseResult getRecommendPlatformList(HttpServletRequest request, HttpServletResponse response) {
		AppResponseResult result = new AppResponseResult();
		try {
			Integer pageNum = NumberUtil.parseInteger(request.getParameter("pageNum"), null);// 页码
			Integer pageSize = NumberUtil.parseInteger(request.getParameter("pageSize"), null);// 每页数量
			logger.info("【PlatformController.getRecommendPlatformList】pageNum:" + StringUtil.toString(pageNum)
					+ ",pageSize:" + StringUtil.toString(pageSize));
			if (pageNum == null || pageSize == null || pageNum <= 0 || pageSize <= 0) {
				result.setCode(AppConstant.Code.FAIL);
				result.setMsg(AppConstant.Msg.FAIL);
				result.setResult("");
				return result;
			}
			List<Map<String, Object>> list = platFormService.getRecommendPlatformList(pageNum, pageSize);
			logger.info("【PlatformController.getRecommendPlatformList】list:" + JSONArray.toJSONString(list));
			result.setCode(AppConstant.Code.SUCCESS);
			result.setMsg(AppConstant.Msg.SUCCESS);
			result.setResult(list);
		} catch (Exception e) {
			result.setCode(AppConstant.Code.ERROR);
			result.setMsg(AppConstant.Msg.ERROR);
			result.setResult("");
			logger.error("【PlatformController.getRecommendPlatformList】异常", e);
		}
		return result;
	}

	/**
	 * 平台详情
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "getPlatformDetail")
	@ResponseBody
	public AppResponseResult getPlatformDetail(HttpServletRequest request, HttpServletResponse response) {
		AppResponseResult result = new AppResponseResult();
		try {
			String platformId = request.getParameter("platformId");
			logger.info("【PlatformController.getPlatformDetail】platFormId:" + StringUtil.toString(platformId));
			if (StringUtil.isEmpty(platformId)) {
				result.setCode(AppConstant.Code.FAIL);
				result.setMsg(AppConstant.Msg.FAIL);
				result.setResult("");
				return result;
			}
			Map<String, Object> map = platFormService.getPlatformDetail(platformId);
			logger.info("【PlatformController.getPlatformDetail】list:" + JSON.toJSONString(map));
			result.setCode(AppConstant.Code.SUCCESS);
			result.setMsg(AppConstant.Msg.SUCCESS);
			result.setResult(map);
		} catch (Exception e) {
			result.setCode(AppConstant.Code.ERROR);
			result.setMsg(AppConstant.Msg.ERROR);
			result.setResult("");
			logger.error("【PlatformController.getPlatformDetail】异常", e);
		}
		return result;
	}

	/**
	 * 判断用户是否第一次点击对应产品
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "getIsFirstClick")
	@ResponseBody
	public AppResponseResult getIsFirstClick(HttpServletRequest request, HttpServletResponse response) {
		AppResponseResult result = new AppResponseResult();
		try {
			String borrowerId = request.getParameter("borrowerId");// 借款人ID
			String platformId = request.getParameter("platformId");// 平台ID
			logger.info("【PlatformController.getIsFirstClick】borrowerId:" + StringUtil.toString(borrowerId)
					+ ",platformId:" + platformId);
			if (StringUtil.isEmpty(borrowerId) || StringUtil.isEmpty(platformId)) {
				result.setCode(AppConstant.Code.FAIL);
				result.setMsg(AppConstant.Msg.FAIL);
				result.setResult("");
				return result;
			}
			Map<String, Object> map = platFormService.getIsFirstClick(borrowerId, platformId);
			logger.info("【PlatformController.getIsFirstClick】map:" + JSON.toJSONString(map));
			result.setCode(AppConstant.Code.SUCCESS);
			result.setMsg(AppConstant.Msg.SUCCESS);
			result.setResult(map);
		} catch (Exception e) {
			result.setCode(AppConstant.Code.ERROR);
			result.setMsg(AppConstant.Msg.ERROR);
			result.setResult("");
			logger.error("【PlatformController.getIsFirstClick】异常", e);
		}
		return result;
	}

}
